<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            height: 300px;
            display: flex;
            justify-content: center;
        }
        .box #item{
            width: 100px;
            height: 100px;
            border-radius: 50%;
            /* background-color: #ccc; */
        }
        .box .red{
            background-color: #f00;
        }
        .box .green{
            background-color:green;
        }
        .box .yellow{
            background-color:yellow;
        }
    </style>
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
    <div class="box">
        <div id="item"></div>
        <!-- <div class="item"></div>
        <div class="item"></div> -->
    </div>
</body>
<script>
    var containerbox = $('#item')
    function red() {
    console.log("red");
    containerbox.removeClass()
    containerbox.addClass('red');
  }
  function green() {
     containerbox.removeClass()
     containerbox.addClass('green');
  }
  function yellow() {
    containerbox.removeClass()
    containerbox.addClass('yellow');
  }
 

  function light(fn,time){
    return new Promise(resolve=>{
        setTimeout(function(){
            fn()
            console.log(time)
            resolve()
        },time)
    })
  }
  

  function step(){
      Promise.resolve().then(()=>{
          return light(red,1000)
      }).then(()=>{
            return light(green,8000)
      }).then(()=>{
            return light(yellow,5000);
      }).then(()=>{
          step()
      })
  }
  step()
</script>
</html>